//
//  ChoseArticleTypeView.m
//  ShowMe
//
//  Created by 123 on 2018/9/14.
//  Copyright © 2018年 nkl. All rights reserved.
//

#import "ChoseArticleTypeView.h"
#import "ClassModel.h"
@interface ChoseArticleTypeView()
@property(nonatomic,strong) UIView * bgView;
@property(nonatomic,strong) UILabel * titleView;
@property(nonatomic,strong) UIButton * closeBtn;
@property(nonatomic,strong) UIButton * pushBtn;
@property(nonatomic,assign) NSInteger selectedID;
@property(nonatomic,weak) UIButton * selectedButton;
@end

@implementation ChoseArticleTypeView

-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = __KNewSize(10);
        _bgView.layer.shadowColor = CGColor(170, 170, 170, 1);
        _bgView.layer.shadowOpacity = 1;
        _bgView.layer.shadowOffset = CGSizeMake(0, 0);
    }
    return _bgView;
}

-(UILabel *)titleView{
    if (!_titleView) {
        _titleView = [[UILabel alloc] init];
        _titleView.textColor = ThemeColor;
        _titleView.text = @"文章类型";
        _titleView.font = __kNFont(20);
    }
    return _titleView;
}

-(UIButton *)closeBtn{
    if (!_closeBtn) {
        _closeBtn = [[UIButton alloc] init];
        [_closeBtn setImage:[UIImage imageNamed:@"close_HL"] forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeViewEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

-(UIButton *)pushBtn{
    if (!_pushBtn) {
        _pushBtn = [[UIButton alloc] init];
        [_pushBtn setTitle:@"确认" forState:UIControlStateNormal];
        [_pushBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _pushBtn.tag = 1996;
        [_pushBtn addTarget:self action:@selector(didClickEvent) forControlEvents:UIControlEventTouchUpInside];
        _pushBtn.layer.cornerRadius = __KNewSize(5);
        _pushBtn.backgroundColor = ThemeColor;
    }
    return _pushBtn;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectedID = -1;
        [self setUpUI];
    }
    return self;
}

-(void)setUpUI{
    [self.bgView addSubview:self.pushBtn];
    [self.bgView addSubview:self.titleView];
    [self addSubview:self.bgView];
    [self addSubview:self.closeBtn];
  
}

-(void)setDataForView:(NSMutableArray *)dataArray{
    
    for(int i = 0 ;i<dataArray.count;i++){
        ClassModel * model = (ClassModel *)dataArray[i];
        CGFloat mW = __KNewSize(80);
        CGFloat mH = __KNewSize(30);
        CGFloat mX = __KNewSize(20)  + (i%3) * (__KNewSize(80) + __KNewSize(10));
        CGFloat mY = __KNewSize(130) + (i/3) * (mH+__KNewSize(40));
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(mX, mY , mW ,mH)];
        btn.tag = i + 100;
        btn.selected = NO;
        [btn setTitle:model.ClassMsg forState:UIControlStateNormal];
        [btn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(selectedType:) forControlEvents:UIControlEventTouchUpInside];
        btn.layer.cornerRadius = __KNewSize(4);
        btn.layer.borderColor = ThemeColor.CGColor;
        btn.layer.borderWidth = __KNewSize(1);
        [self.bgView addSubview:btn];
    }
}

-(void)setSelectedIndex:(NSInteger)index{
    for (UIView * btnView in self.bgView.subviews) {
        if (index+100 == btnView.tag) {
            UIButton * btn = (UIButton * )btnView;
            btn.selected = YES;
            btn.backgroundColor = ThemeColor;
            self.selectedButton = btn;
        }
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(__KNewSize(85));
        make.left.equalTo(self).offset(__KNewSize(38));
        make.right.equalTo(self).offset(__KNewSize(-37));
        make.bottom.equalTo(self).offset(__KNewSize(-117));
    }];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);
        make.top.equalTo(self.bgView).offset(__KNewSize(50));
    }];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_bottom).offset(__KNewSize(20));
        make.centerX.equalTo(self);
        make.width.mas_equalTo(__KNewSize(24));
        make.height.mas_equalTo(__KNewSize(24));
    }];
    [self.pushBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);
        make.bottom.equalTo(self.bgView).offset(__KNewSize(-20));
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(30);
    }];
}

-(void)closeViewEvent{
    [self removeFromSuperview];
}

-(void)selectedType:(UIButton *) sender {
    self.selectedID = sender.tag - 100;
    if (sender!=self.selectedButton) {
        self.selectedButton.selected = NO;
        self.selectedButton.backgroundColor =  UIColor.whiteColor;
        self.selectedButton.layer.borderColor = ThemeColor.CGColor;
        sender.selected = YES;
        sender.backgroundColor = ThemeColor;
        sender.layer.borderColor = UIColor.whiteColor.CGColor;
        self.selectedButton = sender;
    }else{
        self.selectedButton.selected = YES;
    }
    
//    self.selectedID = sender.tag - 100;
//    for (UIView * btnView in self.bgView.subviews) {
//        if (btnView.tag>=100) {
//            if (btnView.tag == sender.tag) {
//                sender.selected = !sender.isSelected;
//                self.selectedID = sender.isSelected ? (sender.tag - 100) : -1;
//                sender.backgroundColor = sender.isSelected ? ThemeColor : UIColor.whiteColor;
//                sender.layer.borderColor = sender.isSelected ? UIColor.whiteColor.CGColor : ThemeColor.CGColor;
//            }else{
//                UIButton * tempBtn = (UIButton *)btnView;
//                if (tempBtn.isSelected == YES) {
//                    tempBtn.selected = NO;
//                    tempBtn.backgroundColor = UIColor.whiteColor;
//                    tempBtn.layer.borderColor = ThemeColor.CGColor;
//                }
//            }
//        }
//    }
}

-(void)didClickEvent{
    
    if ([self.delegate respondsToSelector:@selector(pushButtonClickEvent:currentView:)]) {
        [self.delegate pushButtonClickEvent:self.selectedID currentView:self];
    }
}
@end
